#include "musicslider.h"
#include "ui_musicslider.h"

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    //默认的进度条是0
    curPos = 0;
    maxWidth = width();
    moveSlider();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    //获取对应的鼠标的位置，更新进度条
    curPos = event->pos().x();
    moveSlider();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    //1.需要判断是否在当前这个区域
    QRect rect(0,0,width(),height());
    QPoint pos = event->pos();
    if(!rect.contains(pos))
    {
        return;     //如果不在这个区域，那么移动无效
    }
    //对鼠标左键的移动操作进行处理
    if(event->buttons() == Qt::LeftButton)
    {
        //2.如果移动越界，就需要缩放到最大或者最小的区域
        curPos = event->pos().x();
        if(curPos < 0) curPos = 0;
        if(curPos > maxWidth) curPos = maxWidth;
        //3.更新进度条
        moveSlider();
    }
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    curPos = event->pos().x();
    moveSlider();
    //鼠标释放的时候，就可以通知music类修改对应的操作
    emit setMusicSliderPosition((float)curPos/(float)width());
}

void MusicSlider::moveSlider()
{
//    ui->outLine->setMaximumWidth(curPos);
//    ui->outLine->setGeometry(9,9,curPos,2);
    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),curPos,ui->outLine->height());
}

void MusicSlider::setStep(float bf)
{
    //设置进度条的进度
    curPos = maxWidth * bf;
    moveSlider();
}
